package com.itsu.manage.exception;

import com.itsu.pojo.enums.ResultCode;
import com.itsu.pojo.results.ErrorResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class MyExceptionHandler {

    @ExceptionHandler(MyException.class)
    public ResponseEntity myEx(MyException myException) {
        myException.printStackTrace();
        // 响应500 并且把错误对象返回
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(myException.getErrorResult());
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity ex(Exception exception) {
        exception.printStackTrace();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ErrorResult(ResultCode.ERROR));
    }

}
